package org.sws.types.common.page;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.sws.types.enums.ResponseCode;

import java.util.List;

/**
 * @Author sws
 * @Date 2025/3/28 18:04
 * @description: 列表分页数据对象
 */
@Data
@NoArgsConstructor
public class PageResult<T> {

    /* 总数 */
    private long total;
    /* 列表数据 */
    private List<T> data;
    /* 消息状态码*/
    private String code;
    /* 消息内容*/
    private String info;

    public PageResult(Long total, List<T> data) {
        this.total = total;
        this.data = data;
    }
    public static <T> PageResult<T> build(PageInfo<T> pageInfo) {
        PageResult<T> rspData = new PageResult<>();
        rspData.setCode(ResponseCode.SUCCESS.getCode());
        rspData.setInfo(ResponseCode.SUCCESS.getInfo());
        rspData.setData(pageInfo.getList());
        rspData.setTotal(pageInfo.getTotal());
        return rspData;
    }

    public static <T> PageResult<T> build(Page<T> page) {
        PageResult<T> rspData = new PageResult<>();
        rspData.setCode(ResponseCode.SUCCESS.getCode());
        rspData.setInfo(ResponseCode.SUCCESS.getInfo());
        rspData.setData(page.getResult());
        rspData.setTotal(page.getTotal());
        return rspData;
    }

    public static <T> PageResult<T> build(List<T> list) {
        PageResult<T> rspData = new PageResult<>();
        rspData.setCode(ResponseCode.SUCCESS.getCode());
        rspData.setInfo(ResponseCode.SUCCESS.getInfo());
        rspData.setData(list);
        rspData.setTotal(list.size());
        return rspData;
    }

    public static <T> PageResult<T> build() {
        PageResult<T> rspData = new PageResult<>();
        rspData.setCode(ResponseCode.SUCCESS.getCode());
        rspData.setInfo(ResponseCode.SUCCESS.getInfo());
        return rspData;
    }
}
